package cn.ante.order.controller;

import cn.ante.basic.utils.LoginContext;
import cn.ante.order.service.IOrderProductService;
import cn.ante.order.domain.OrderProduct;
import cn.ante.order.query.OrderProductQuery;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import cn.ante.user.domain.Logininfo;
import cn.ante.user.domain.User;
import cn.ante.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    public IOrderProductService orderProductService;

    @Autowired
    private IUserService userService;

    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.add(orderProduct);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderProduct get(@PathVariable("id")Long id)
    {
        return orderProductService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProduct> list(){

        return orderProductService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderProduct> json(@RequestBody OrderProductQuery query)
    {
        return orderProductService.findAll(query);
    }

    @PostMapping("/pay")
    public AjaxResult json(@RequestBody OrderProduct orderProduct, HttpServletRequest request) {
        try {
            // 查找到当前登陆人,获取到登陆用户信息
            Logininfo logininfo = LoginContext.getLoginifo(request);
            User user = userService.loadByLogininfoId(logininfo.getId());
            // 将寻主信息中的useid绑定上
            orderProduct.setUserId(user.getId());
            orderProductService.pay(orderProduct);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }
}
